import type { Ref } from "vue";
import type { MenuDetailResult } from "@/api/types";
import { ref } from "vue";

interface UseMenuFormDialog {
    /** 对话框显隐 */
    dialogVisible: Ref<boolean>;
    /** 当前编辑数据 */
    editData: Ref<MenuDetailResult | null>;
    /** 父菜单 ID */
    parentId: Ref<string | null>;
    /** 打开新增弹窗 */
    openAddDialog: (pId: string | null) => void;
    /** 打开编辑弹窗 */
    openEditDialog: (row: MenuDetailResult) => void;
}

export function useMenuFormDialog(): UseMenuFormDialog {
    const dialogVisible = ref(false);
    const editData = ref<MenuDetailResult | null>(null);
    const parentId = ref<string | null>(null);

    /** 打开新增弹窗 */
    function openAddDialog(pId: string | null): void {
        editData.value = null;
        parentId.value = pId;
        dialogVisible.value = true;
    }

    /** 打开编辑弹窗 */
    function openEditDialog(row: MenuDetailResult): void {
        editData.value = row;
        parentId.value = row.parentId || null;
        dialogVisible.value = true;
    }

    return {
        dialogVisible,
        editData,
        parentId,
        openAddDialog,
        openEditDialog,
    };
}
